<?php
	$tplEdit = new XTemplate('view/product/edit.htm');
	
	//Get info
	$productId = _POST('id');
	$tplEdit->assign('baseUrl', $baseUrl);
	$error 			= '';
	$categoryId 	= _POST('category_id');
	$productName	= _POST('product_name');
	$price			= _POST('product_price');
	$size			= _POST('size');
	$description 	= _POST('description');	
	if($productId){
		$sql = "SELECT * FROM tblproduct WHERE product_id = $productId";
		$product = $db->fetchOne($sql);
		if($product['at_home']){
			$product['chkhome'] = 'checked="checked"';
		}
		if($product['at_slider']){
			$product['chkslider'] = 'checked="checked"';
		}
		if($product['at_pronew']){
			$product['chkpronew'] = 'checked="checked"';
		}
		if($product['at_prosale']){
			$product['chksales'] = 'checked="checked"';
		}
		if($product['special']){
			$product['chkspecial'] = 'checked="checked"';
		}
		if($product['bestseller']){
			$product['bestseller'] = 'checked="checked"';
		}
		$tplEdit->assign('product', $product);
		
		$categoryId =  $product['category_id'];
		
		//Product Images
		$sql = "SELECT * FROM tblproduct_img WHERE product_id = $productId";
		$images = $db->fetchAll($sql);
		foreach ($images as $image) {
			if($image['thumbnail']){
				$image['thumbcheck'] = 'checked="checked"';
			}
			if($image['small_img']){
				$image['smallcheck'] = 'checked="checked"';
			}
			if($image['basic_img']){
				$image['basecheck'] = 'checked="checked"';
			}
			$tplEdit->insert_loop('edit.listimg', array('listimg'=>$image));
		}
		// Product color
		$colors = $db->fetchAll("SELECT * FROM tblproduct_color WHERE product_id = $productId GROUP BY color_code");
		foreach($colors as $color){
			
			$colorimgs = $db->fetchAll("SELECT * FROM tblproduct_color WHERE color_id = ".$color['color_id']);
			foreach($colorimgs as $colorimg){
				$tplEdit->insert_loop('edit.listcolor.listimgcolor', array('listimgcolor'=>$colorimg));
			}
			$tplEdit->insert_loop('edit.listcolor', array('listcolor'=>$color));
		}
		if($_POST){
			$product 	= _POST('product');
			$atHome 	= _POST('at_home');
			$atSlider 	= _POST('at_slider');
			$atPronew	= _POST('at_pronew');
			$atProsale	= _POST('at_prosale');
			$productSpecial = _POST('product_special');
			$bestseller = _POST('product_bestseller');
			//Update table product image
			if ($product['delete']) {
			
				$imgId = implode(',', $product['delete']);
				$images = $db->fetchAll("SELECT * FROM tblproduct_img WHERE img_id IN ($imgId) ");
				foreach( $images as $image ) {
					if(file_exists('..'.$image['img_url'] )){
						unlink('..'.$image['img_url']);
					}
				}
				
				$db->delete("tblproduct_img", " img_id IN ($imgId) ");
			
			}
			if($product['order']){
			
				$imgs = $product['order'];
				foreach ( $imgs as $id=>$order) {
					
					$data = array('img_order' => $order);
					$db->update('tblproduct_img', $data, " img_id = $id ");
				}
			}
			if($product['label']){
				$labels = $product['label'];
				foreach ( $labels as $id=>$label) {
					
					$data = array('img_label' => $label);
					$db->update('tblproduct_img', $data, " img_id = $id ");
				}
			}
			if($product['image'] ){
				
				$data = array('basic_img' => 1);
				$db->update('tblproduct_img', $data, " img_id = ".$product['image']);
			}
			//Validate form
		
			if($categoryId == '-1'){
				$error .= '<li>Bạn chưa chọn danh mục.</li>';
			}
			if($productName == ''){
				$error .= '<li>Bạn chưa nhập tên sản phẩm.</li>';
			}
			if($error == ''){
				$data = array();
				if(_POST('chk')){
					$relatedProduct = implode(',', _POST('chk'));
				}else{
					$relatedProduct = _POST('related_product');
				}
				
				$data['category_id'] 	= $categoryId;
				$data['product_name'] 	= $productName;
				$data['price'] 			= $price;
				$data['size'] 			= $size;
				$data['description'] 	= $description;
				$data['at_home'] 		= $atHome ? $atHome : 0;
				$data['at_slider'] 		= $atSlider ? $atSlider :0;
				$data['at_pronew'] 		= $atPronew ? $atPronew :0;
				$data['at_prosale'] 	= $atProsale ? $atProsale :0;
				$data['special'] 		= $productSpecial ? $productSpecial :0;
				$data['bestseller'] 	= $bestseller ? $bestseller :0;
				$data['related_product']= $relatedProduct;
				$db->update('tblproduct', $data, " product_id=$productId");
				$db->update('tblproduct_img', array('product_id'=>$productId), ' product_id IS NULL ');
				$db->update('tblproduct_color', array('product_id'=>$productId), ' product_id IS NULL ');
				redir($baseUrl.'/admin/?c=product&a=list');
			}else{
				if($product['idimg']){
					$imgId = implode(',', $product['idimg']);
					$images = $db->fetchAll("SELECT * FROM tblproduct_img WHERE img_id IN ($imgId)");
					foreach ( $images as $image ) {
						//checked="checked"
						if($image['thumbnail'] == 1){
							$image['thumbcheck'] = 'checked="checked"';
						}
						if($image['small_img'] == 1){
							$image['smallcheck'] = 'checked="checked"';
						}
						if($image['basic_img'] == 1){
							$image['basecheck'] = 'checked="checked"';
						}
						$tplPro->insert_loop('add.listimg', array('listimg'=>$image));
					}
				}
				
				$tplPro->assign('productName', $productName);
				$tplPro->assign('price', $price);
				$tplPro->assign('size', $size);
				$tplPro->assign('description', $description);
				//$tplPro->assign('error', $error);
				//$tplPro->assign('error', $error);
				$tplPro->assign('error', $error);
			}
		}
	}
	$sltCategory = getCombo('tblcategory', 'category_id', 'category_name', 'category_id', '', $categoryId, null, null, 'input-text-medium');
	$tplEdit->assign('sltCategory', $sltCategory);
	$tplEdit->parse('edit');
	$content = $tplEdit->text('edit');